package cloud.xlh;

import cloud.xlh.infrastructure.file.entity.MountSettingEntity;
import cloud.xlh.service.file.service.FileService;
import cloud.xlh.service.file.service.RemoteFileService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/8/6 21:07
 * @ Description:
 */
public class LoadDataSpringApplicationRunListener implements SpringApplicationRunListener {

    private final SpringApplication application;
    private final String[] args;

    public LoadDataSpringApplicationRunListener(SpringApplication sa, String[] args) {
        this.application = sa;
        this.args = args;
    }

    public void running(ConfigurableApplicationContext context) {
        FileService fileService = (FileService)context.getBean("fileService");
        RemoteFileService remoteFileService = (RemoteFileService)context.getBean("remoteFileService");
        List<MountSettingEntity> mountSettingEntities = fileService.loadAllMountSetting();
        ConcurrentHashMap<String, String> mountDirMpMap = remoteFileService.getMountDirMpMap();
        mountSettingEntities.stream().forEach(mountSettingEntity
                -> mountDirMpMap.put(mountSettingEntity.getMpLocalDirPath()
                , mountSettingEntity.getMpRemoteDirPath()));
    }
}
